home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Linked List Template Classes
/
Linked Lists Ä.sit
/
Linked Lists ƒ
/
source code
/
LinkedLists.h
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-02
|
9KB
|
225 lines
#pragma once
/********************************************************************************************
Class ListItem is used internally by the ListClasses; you don't have to worry about it.
It simply is a template class which will hold the class you're storing in the list and will
point to the next object in the list
********************************************************************************************/
template <class itemClass> class ListItem {
public:
itemClass item;
ListItem *next;
};
/********************************************************************************************
General List Info
Ñ "itemRecord" refers to the specific variable/class/struct type
when an instance of this template class is created
********************************************************************************************/
/********************************************************************************************
SlimListClass
SlimListClass is the most basic of all the Lists, providing simple Push and Pop operations.
(It will create a stack, with only the top item accessible, no sorting involved.)
This class will also "clean up" after itself, deallocating all the memory it allocates when
it is terminated.
Function Usage
var.Push(itemRecord theItem);
Ñ will "push" the item on to the top of the stack
Ñ returns 1 if successful, 0 if not (memory couldn't be allocated)
item=var.Pop();
Ñ "pops" the top item - takes it off the stack and returns item
var.DeleteAll();
Ñ deletes all entries in stack
Ñ also called when variable is destroyed (e.g., local variable in function dieing when
function is done)
********************************************************************************************/
template <class itemRecord> class SlimListClass {
protected:
ListItem<itemRecord> *theList;
public:
SlimListClass() { theList=theList->next=nil; }
~SlimListClass() { DeleteAll(); }
Boolean Push(itemRecord &theItem);
itemRecord Pop();
void DeleteAll();
};
/********************************************************************************************
SlimPlusClass
SlimPlusClass is essentially the same as its base class, SlimListClass, but with a query
function, as well as a useful function, "XInList", explained below. Because of the
latter function, this class needs two object types when creating it. The reason will
become clear with the explanation of "XInList".
Special Note:
Because this class compares the objects you specify when creating it (the itemRecord
objects) you must provide for object comparison in your object class (with ==, only).
example
class declaration {
// var declarations in class
// function declarations in class
Boolean operator == (myClass rightObject) { return *this==rightObject; }
};
(you can compare whatever feature of the objects with each other as is necessary)
Function Usage
var.ItemInList(theItem)
Ñ is passed an item, of type itemRecord, returns if item is in list (1 or 0)
var.XInList(thisComparisonObject, * &theItem)
Ñ passed an object to compare theItem with, XInList will return if item is in list
Ñ if the comparison object is in the list, theItem parameter will be set to a pointer to
that object
Ñ examples/suggestions for usage:
I create my own dialog and window classes, and throw them into a linked list. When I
receive update/activate/etc. events, I pass the WindowPtr in the first parameter and
a dialog/window class pointer in the second parameter. The SlimPlusClass will then
compare these two with each item in the list, returning the first match it finds in
the second paramater, or 0 if it's not found. If it finds a match, I simply call the
update procedure in the returned object.
Important note: You must have a logic comparison in the itemRecord class for the
comparison class. For example, suppose you create SlimPlusList<myWindowClass, WindowPtr>.
Within myWindowClass, you must overload the "==" operator for myWindowClass==WindowPtr
logic tests.
Ñ a pointer is returned so that changes will be made to _that_ object; if you simply used
a reference variable of the object, a copy of that object would be made so changes to
the object returned would not be made to the object in the list
********************************************************************************************/
template <class itemRecord, class comparisonObject> class SlimPlusClass : public SlimListClass<itemRecord> {
public:
Boolean ItemInList(itemRecord &theItem);
Boolean XInList(comparisonObject theObject, itemRecord * &theItem);
};
/********************************************************************************************
ListClass
ListClass is a the most featured list here, capable of sorting entries as they're input,
retrieving any object from the list, and providing information on the number of items
in the list. ListClass also has a placeholder, pointing to the current place in the list.
Its significance is discussed below.
Funcation Usage
Push, Pop, and DeleteAll are all the same as the SlimListClass defined above.
var.NumItems()
Ñ returns the number of items in the list
var.Add(theItem)
Ñ instead of pushing the item on the stack, Add adds the item to the end of the list
var.AddSort(theItem)
Ñ AddSort will sort the input as it's entered, provided you've defined (overloaded) all
the logical operators for your class ( <, >, ==, != )
Ñ Using AddSort to enter all items will create an ascending list (lesser objects at
the stack top, down to the greater items)
Ñ Important note: AddSort will sort only the piece that's entered, not the whole list.
That is, if you Push some items on, Add others, then AddSort an item, the list will
not be sorted, and the item will be placed after the first object it's greater than
var.Delete(theItem)
Ñ Given an item, it will purge the item, and the memory the item occupied, from the list
var.SetToTop()
Ñ This sets the placeholder to the top item
Ñ Returns 1 if there is a stack top (there is a list), 0 otherwise
var.GetPlaceItem(* &theItem)
Ñ This returns a pointer to the itemRecord object currently pointed to in the list in
second parameter
Ñ Returns a non-zero value if there is an item there
var.GetNextItem(* &theItem)
Ñ Returns a pointer to the next itemRecord object in the list in second parameter
Ñ Returns a non-zero value if there is a "next" object
Ñ Sets the placeholder to the next item
var.GetItemX(itemNumber, * &theItem)
Ñ Returns a pointer to the xth item (first, second, etc.) in a list in second parameter
Ñ Returns 1 if there is an item x, 0 if not
Suggestions for using placeholder-related functions
I use this list class for data which should be sorted as well as for data which shouldn't be.
A good example of the former use is when you have to find the median (middle number) in a set
of inputted data. As the user inputs the data, I AddSort() it to a list. When I need to
report back the median, I query the number of items in the list with NumItems(), then use
this number to GetItemX().
When the order of the data entered should be preserved, I simply use Add() which will keep
the first item entered at the top of the list. Then, when I need to retrieve each item of
the list, I SetToTop() which sets the placeholder to the top of the list, GetPlaceItem(),
which retrieves the top item if it's there, then do a simple while loop that goes until
GetNextItem returns 0 (actually nil), retrieving each successive item in the list.
********************************************************************************************/
template <class itemRecord> class ListClass {
protected:
ListItem<itemRecord> *stackTop;
ListItem<itemRecord> *placeHolder;
int numberOfItems;
public:
ListClass() { placeHolder=stackTop=nil; numberOfItems=0; }
~ListClass() { DeleteAll(); }
int NumItems() { return numberOfItems; }
Boolean AddSort(itemRecord &theItem);
Boolean Add(itemRecord &theItem);
Boolean Push(itemRecord &theItem);
itemRecord Pop();
void Delete(itemRecord theItem);
void DeleteAll();
Boolean SetToTop() { placeHolder=stackTop; if (stackTop) return 1; return 0;}
ListItem<itemRecord> *GetPlaceItem(itemRecord * &theItem);
ListItem<itemRecord> *GetNextItem(itemRecord * &theItem);
Boolean GetItemX(short itemNumber, itemRecord * &theItem);
};